import React, { useReducer } from "react";
import { useNavigate } from "react-router-dom";
import { Form } from "antd";
import axios from "axios";
import CancelModel from "../../../common/CancelModel";
import getFields from "./getFields.js";
import FormEdit from "../../../common/FormEdit";


import "./index.css";


const initialState = {
  cancelModelStatus: false,
  };
const reducer = (state, action) => {
  console.log("模态框传入",action);
    switch (action.type) {
      case 'changeCancelModel':
          return {
            ...state,
            cancelModelStatus: action.status,
           
          };
          default:
    
    }
  };


export default function PoiManageList() {
     /**
  * 设置取消按钮的模态框
  */
      const [store, dispatch] = useReducer(reducer, initialState);

  let navigation=useNavigate();


  const [form] = Form.useForm();
  function saveCui(values){
    axios({
      headers: {
        'tenantId':500,
        "userId":11111
    },
      method: 'post',
      url:`http://localhost:9101/product`,
      data:{
      ...values
      }
    }).then((data)=>{

      return true;
       }).catch((data)=>{
        console.log("错误信息",data); 
       })
}


  const onFinish = (values) => {
    /**
     * 处理加料组数据
     */
   let obj=[{
     name:"加菜",
     options: values.accessoryGroups,
    }]
  
    if(values.accessoryGroups && values.accessoryGroups.length>0)
    {
      values.accessoryGroups=obj;
    
    }
    
    saveCui(values);
    
  };


  return (
    <div>
       <FormEdit
        form={form}
        onFinish={onFinish}
        initialValues={{
          "minSalesQuantity":1,
          "increaseSalesQuantity":1,
        }}
        name="添加菜品页面"
        getFields={getFields()}
        resetFields={() => {
          dispatch({type:'changeCancelModel', status:true});
        }}
        saveBack={() => {
          form.submit((e)=>{
            let obj=[{
              name:"加菜",
              options: e.accessoryGroups,
             }]
             if(e.accessoryGroups && e.accessoryGroups.length>0)
             {
               e.accessoryGroups=obj;
             }
             saveCui(e);
          });
          navigation('/cui-manage/cuiList');
        }}
      />
      <CancelModel store={store} dispatch={dispatch} path="/cui-manage/cuiList"/>

    </div>
  );
}
